//
//  RecommendViewController.m
//  FairyDream
//
//  Created by dllo on 15/8/20.
//  Copyright (c) 2015年 dreams. All rights reserved.
//

#import "RecommendViewController.h"
#import "headerCollectionView.h"
#import "AsyncGETConnection.h"
#import "Define.h"
#import "SubjectTableViewCell.h"
#import "DiscountTableViewCell.h"
#import "DiscountImageTableViewCell.h"
#define Width self.view.frame.size.width
#define Height self.view.frame.size.height
@interface RecommendViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) headerCollectionView *headerView;
@end

@implementation RecommendViewController

- (void)dealloc
{
    [_discout_subjectArray release];
    [_discoutArray release];
    [_returnArray release];
    [_threeImageArray release];
    [_slideArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.slideArray = [NSMutableArray array];
        self.threeImageArray = [NSMutableArray array];
        self.returnArray = [NSMutableArray array];
        self.discout_subjectArray = [NSMutableArray array];
        self.discoutArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    [self createTableView];
    [self startConnection];
}



- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.headerView = [[headerCollectionView alloc] initWithFrame:CGRectMake(0, 0, Width, 300)];
    [_headerView createPage];
    self.tableView.tableHeaderView = self.headerView;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _returnArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *cellI = @"cellI";
        SubjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellI];
        if (!cell) {
            cell = [[[SubjectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellI]autorelease];
        }
        cell.customArray = _threeImageArray;
        [cell.collecView reloadData];
        return cell;
    }
    else if (indexPath.row == 1)
    {
        static NSString *cellII = @"cellII";
        DiscountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellII];
        if (!cell) {
            cell = [[[DiscountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellII]autorelease];
        }
        cell.aImageArray = _discout_subjectArray;
        [cell setMessage];
        return cell;
    }
    else if (indexPath.row == 2)
    {
        static NSString *cellIII = @"cellIII";
        DiscountImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIII];
        if (!cell) {
            cell = [[[DiscountImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIII]autorelease];
        }
        
        cell.fourImagesArray = _discoutArray;
        
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 350;
    }
    else if (indexPath.row == 1)
    {
        return 200;
    }
    else
    {
        return 400;
    }
}

- (void)startConnection
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"qyer_ios" forKey:@"client_id"];
    [dic setObject:@"cd254439208ab658ddf9" forKey:@"client_secret"];
    [dic setObject:@"20" forKey:@"count"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"App%2520Store" forKey:@"track_app_channel"];
    [dic setObject:@"6.5.5" forKey:@"track_app_version"];
    [dic setObject:@"iPhone7%2C1" forKey:@"track_device_info"];
    [dic setObject:@"216B2645-C28F-42CC-A74E-17449CA8DF1A" forKey:@"track_deviceid"];
    [dic setObject:@"ios%25208.1.3" forKey:@"track_os"];
    [dic setObject:@"1" forKey:@"v"];
    [AsyncGETConnection asyncGETConnectionWithURL:TJ_QY pamarters:dic block:^(NSData *data) {
        NSDictionary *root = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = [root objectForKey:@"data"];
        NSArray *slideArray = [dataDic objectForKey:@"slide"];
        for (NSDictionary *photoDic in slideArray) {
            [_slideArray addObject:photoDic];
            }
        _headerView.pictureArray = _slideArray;
        [_headerView.collectionView reloadData];
        // 界面刷新之后调用定时器的方法
        [_headerView addTimer];
        
        NSArray *subjectArray = [dataDic objectForKey:@"subject"];
        for (NSDictionary *subDic in subjectArray) {
            [_threeImageArray addObject:subDic];
        }
        
        NSArray *discount_subArray = [dataDic objectForKey:@"discount_subject"];
        for (NSDictionary *dis_subDic in discount_subArray) {
            [_discout_subjectArray addObject:dis_subDic];
        }
        
        NSArray *discountArray = [dataDic objectForKey:@"discount"];
        for (NSDictionary *discountDic in discountArray) {
            [_discoutArray addObject:discountDic];
        }
        [_tableView reloadData];
    }];
    [_returnArray addObject:_threeImageArray];
    [_returnArray addObject:_discout_subjectArray];
    [_returnArray addObject:_discoutArray];
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
